
import { ConfigProvider } from "../../providers/config/config";
import { StorageProvider } from "../../providers/storage/storage";

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { ValidateProvider } from "../../providers/validate/validate";
import { HttpServicesProvider } from "../../providers/http-services/http-services";
import { UtilsProvider } from "../../providers/utils/utils";

@IonicPage({
    segment: 'member/collect/:kind',
    defaultHistory: ['MemberPage']
})
@Component({
    selector: 'page-person-collecttask',
    templateUrl: 'person-collecttask.html',
})
export class PersonCollecttaskPage {
    kind = '';
    titleType = '任务';
    sendData = {};
    //评论框 默认隐藏
    commentData = {};
    constructor(
        public navCtrl: NavController,
        public navParams: NavParams,
        public config: ConfigProvider,
        public validate: ValidateProvider,
        public storage: StorageProvider,
        public httpService: HttpServicesProvider,
        public util: UtilsProvider
    ) {
    }

    //判断用户是否登录
    ionViewCanEnter() {
        return this.validate.checkLogin();
    }

    ionViewWillEnter() {
        this.kind = this.navParams.get('kind');
        switch (this.kind) {
            case 'information':
                this.titleType = '信息';
                break;
            case 'resume':
                this.titleType = '简历';
                break;
            case 'guide':
                this.titleType = '指南';
                break;
            default:
                this.titleType = '任务';
                break;
        }
        this.getData(null);
    }

    page = 1;
    lists = [];

    // 获取数据
    getData(infiniteScroll) {
        let infoUrl = "";
        if (this.kind == 'resume') {
            infoUrl = "/talents/my_resume_collect"
            this.sendData = {
                token: this.config.getToken(),
                kind: this.kind,
                page: this.page
            };
        } else if (this.kind == 'guide') {
            infoUrl = "/user_info/my_collect";
            this.sendData = {
                type_id: "27,54",
                page: this.page,
                token: this.config.getToken(),
            }
        } else {
            infoUrl = "/user_info/my_collect";
            this.sendData = {
                token: this.config.getToken(),
                kind: this.kind,
                page: this.page
            };
        }
        this.httpService.requestPostData(infoUrl, this.sendData, (response) => {
            //全部数据
            let data = response['data'];
            this.lists = this.lists.concat(data['lists']);
            this.page++;
            if (infiniteScroll) {
                infiniteScroll.complete();
                /*请求完成数据更新页面*/
                if (data['lists'].length < data['page_size']) {   /*没有数据 禁用上拉更新*/
                    infiniteScroll.enable(false);
                }
            }
        });
    }

    doInfinite(infiniteScroll) {
        this.getData(infiniteScroll);
    }

    /**
     * 跳转到任务详情
     * */
    goTaskDetail(amoy_id) {
        this.navCtrl.push('PostDetailPage', { id: amoy_id });
    }

    /**
     * 跳转到信息详情
     * */
    goInformationDetail(amoy_id) {
        this.navCtrl.push('InformationDetailPage', { id: amoy_id });
    }


    // 查看简历

    resumeDetail(resume_id) {
        this.httpService.requestPostData("/user_info/check_level", {
            token: this.config.getToken()
        }, (response) => {
            if (response['error'] == 200 && response['reg_step'] == 2) {
                //高级用户处理
                //查询是否为收费信息，是否已付费
                this.httpService.requestPostData("/talents/resume_detail", {
                    token: this.config.getToken(),
                    resume_id: resume_id
                }, data => {
                    if (data['error'] === 300) {
                        this.util.presentConfirm(data['message'], () => {
                            this.httpService.requestPostData('/talents/resume_pay', {
                                token: this.config.getToken(),
                                resume_id: resume_id
                            }, res => {
                                if (res['error'] == 200) {
                                    this.navCtrl.push('ResumeDetailPage', { resume_id: resume_id });
                                    return false;
                                } else {
                                    this.util.presentAlert(res['message']);
                                    return false;
                                }
                            })
                        });
                        return false;
                    } else if (data['error'] == 200) {
                        this.navCtrl.push('ResumeDetailPage', { resume_id: resume_id });
                        return false;
                    } else {
                        this.util.presentAlert(data['message']);
                    }
                });
            } else {
                this.util.presentConfirm('该功能为高级用户权限，现在去完善注册信息？', () => {
                    this.navCtrl.push('RegisterPage');
                });
            }
        });

    }

    // 跳转到指南页面
    goDetail(type, amoy_id) {
        switch (type) {
            case 27:
                this.navCtrl.push('ArticleDetailPage', { id: amoy_id, type_id: type });
                break;
            default:
                this.navCtrl.push('ArticleDetailPage', { id: amoy_id, type_id: type });
                break;
        }

    }

    /**
     * 删除收藏
     * */
    delCollect(amoy_id, index) {
        this.util.presentConfirm('确定删除吗？', () => {
            let apiUrl = '/task_info/collect_option';
            if (this.kind == 'resume') {
                apiUrl = "/talents/resume_collect"
            }
            this.httpService.requestPostData(apiUrl, {
                resume_id: amoy_id,
                amoy_id: amoy_id,
                token: this.config.getToken(),
                type: 1
            }, (response) => {
                if (response['code'] == 200) {
                    this.lists.splice(index, 1);
                }
            });
        });
    }

}
